package com.oocl.venus.service.impl;

import java.util.Iterator;
import java.util.List;

import com.oocl.venus.dao.DepartmentDao;
import com.oocl.venus.dao.PersonDao;
import com.oocl.venus.entity.Person;
import com.oocl.venus.service.PersonService;
import com.oocl.venus.utils.BeanFactory;
import com.oocl.venus.utils.FilesUtils;



public class PersonServiceImpl implements PersonService {
	
	private BeanFactory beanFactory;
	private DepartmentDao departmentDao;
	private PersonDao personDao;
	
	public PersonServiceImpl() {
		try {
			beanFactory = new BeanFactory();
			personDao = beanFactory.getInstance("personDao");
			departmentDao = beanFactory.getInstance("departmentDao");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public String add(String data) {
		try {
			String[] keyValues = data.split(",");//split the person properties by ","
			Person person = new Person();
			for (int i = 0; i < keyValues.length; i++) {
				String[] currentData = keyValues[i].trim().split(":");
				
				if (currentData[0].equals("pname")) {
					person.setPname(currentData[1]);
				}
				if (currentData[0].equals("pid")) {
					int id = Integer.parseInt(currentData[1]);
					person.setPid(id);//?
				}
				if(currentData[0].equals("birthday")){
					person.setBirthday(currentData[1]);
				}
				if(currentData[0].equals("tel")){
					person.setTel(currentData[1]);
				}
				if(currentData[0].equals("did")){
					int did = Integer.parseInt(currentData[1]);
					if(departmentDao.loadDepartment(did)!=null) {
						person.setDid(did);
						continue;
					}
					return "Did does not exist,add fail";
				}
				if(currentData[0].equals("salary")){
					int salary = Integer.parseInt(currentData[1]);
					person.setSalary(salary);
				}
			}
			
			personDao.addPerson(person);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "Add fail";
		}
		
		return "Add successfully";
	}

	public String delete(String data) {
		String[] currentData = data.split(":");
		int id = Integer.parseInt(currentData[1]);
		if(personDao.loadPerson(id)!=null){
			personDao.deletePerson(id);
			return "Delete successfully";
		}
		return "Pid does not exist,Delete fail";
	}

	public String update(String data) {
		try {
			String[] keyValues = data.split(",");//split the person properties by ","
			Person person = new Person();
			for (int i = 0; i < keyValues.length; i++) {
				String[] currentData = keyValues[i].trim().split(":");
				
				if (currentData[0].equals("pname")) {
					person.setPname(currentData[1]);
				}
				if (currentData[0].equals("pid")) {
					int id = Integer.parseInt(currentData[1]);
					person.setPid(id);
				}
				if(currentData[0].equals("birthday")){
					
					person.setBirthday(currentData[1]);
				}
				if(currentData[0].equals("tel")){
					person.setTel(currentData[1]);
				}
				if(currentData[0].equals("did")){
					int did = Integer.parseInt(currentData[1]);
					if(departmentDao.loadDepartment(did)!=null) {
						person.setDid(did);
						continue;
					}
					return "Did does not exist,update fail";
				}
				if(currentData[0].equals("salary")){
					int salary = Integer.parseInt(currentData[1]);
					person.setSalary(salary);
				}
			}
			personDao.updatePerson(person);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "Update fail";
		}
		return "Update successfully";
	}

	public String findAll() {
		String content = "";
		List<Person> persons = personDao.showAllPerson();
		Iterator<Person> iterator = persons.iterator();
		StringBuffer personsString = new StringBuffer();
		while (iterator.hasNext()) {
			Person person = iterator.next();
			String string = "<tr><td>" + String.valueOf(person.getPid()) + "</td><td>" + person.getPname() + "</td><td>"
					+ person.getBirthday() + "</td><td>" + person.getTel() + "</td><td>"
					+ String.valueOf(person.getSalary()) + "</td></tr>";
			personsString.append(string);
		}
		content = FilesUtils.file("index.html");
		content = content.replace("{{Person}}", personsString);
		return content;
	}

	public String find(String pid) {
		String content = "";
		int id = Integer.parseInt(pid);
		Person person = null;
		person = personDao.loadPerson(id);
		if (person != null) {
			content = FilesUtils.file("index.html");
			String string = "<tr><td>" + String.valueOf(person.getPid()) + "</td><td>" + person.getPname() + "</td><td>"
					+ person.getBirthday() + "</td><td>" + person.getTel() + "</td><td>"
					+ String.valueOf(person.getSalary()) + "</td></tr>";
			content = content.replace("{{Person}}", string);
		}
		return content;
	}
	public String findPerInDept(String data){
		String[] currentData = data.split(":");
		int did = Integer.parseInt(currentData[1]);
		if(departmentDao.loadDepartment(did)==null) return "Did does not exist, load fail;";
		List<Person> p = departmentDao.showDepartmentAll(did);
		Iterator<Person> p1 = p.iterator();
		String result = "";
		while(p1.hasNext()){
			Person pNext = p1.next();
			int pId = pNext.getPid();
			int pDid = pNext.getDid();
			int pSalary = pNext.getSalary();
			result += "pid:"+String.valueOf(pId)+" pname:"+pNext.getPname()+" birthday:"+pNext.getBirthday()+" tel:"+pNext.getTel()+" did:"+String.valueOf(pDid+" salary:"+String.valueOf(pSalary)+"\r\n");
		}
		return result;
	}

}
